home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / MacShell / Menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-04  |  9.1 KB  |  368 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:     MacShell
  5. ** File:        menu.c
  6. ** Written by:  Eric Soldan
  7. **
  8. ** Copyright © 1990-1991 Apple Computer, Inc.
  9. ** All rights reserved.
  10. */
  11.  
  12.  
  13.  
  14. /*****************************************************************************/
  15.  
  16.  
  17.  
  18. #include "MacShell.h"            /* Get the MacShell includes/typedefs, etc.    */
  19. #include "MacShellCommon.h"        /* Get the stuff in common with rez.        */
  20. #include "MacShell.protos"        /* Get the prototypes for MacShell.            */
  21.  
  22. #ifndef __DESK__
  23. #include <Desk.h>
  24. #endif
  25.  
  26. #ifndef __ERRORS__
  27. #include <Errors.h>
  28. #endif
  29.  
  30. #ifndef __LISTCONTROL__
  31. #include "ListControl.h"
  32. #endif
  33.  
  34. #ifndef __MEMORY__
  35. #include <Memory.h>
  36. #endif
  37.  
  38. #ifndef __MENUS__
  39. #include <Menus.h>
  40. #endif
  41.  
  42. #ifndef __TEXTEDITCONTROL__
  43. #include "TextEditControl.h"
  44. #endif
  45.  
  46. #ifndef __TOOLUTILS__
  47. #include <ToolUtils.h>
  48. #endif
  49.  
  50. #ifdef THINK_C
  51. #include "Utilities.h"
  52. #else
  53. #ifndef __UTILITIES__
  54. #include <Utilities.h>
  55. #endif
  56. #endif
  57.  
  58.  
  59.  
  60. /*****************************************************************************/
  61.  
  62.  
  63.  
  64. extern Boolean    gQuitApplication;
  65. extern Boolean    gHasAppleEvents;
  66.  
  67.  
  68.  
  69. /*****************************************************************************/
  70. /*****************************************************************************/
  71.  
  72.  
  73.  
  74. /* Enable and disable menus based on the current state.  The user can only
  75. ** select enabled menu items.  We set up all the menu items before calling
  76. ** MenuSelect or MenuKey, since these are the only times that a menu item can
  77. ** be selected.  Note that MenuSelect is also the only time the user will see
  78. ** menu items.  This approach to deciding what enable/ disable state a menu
  79. ** item has the advantage of concentrating all the decision-making in one
  80. ** routine, as opposed to being spread throughout the application.  Other
  81. ** application designs may take a different approach that is just as valid.
  82. */
  83.  
  84. #pragma segment Menu
  85. void    AdjustMenus(void)
  86. {
  87.     WindowPtr        window;
  88.     MenuHandle        menu;
  89.     short            numWindows;
  90.     Boolean            maxWindows, menuEnabled, redrawMenuBar;
  91.     static Boolean    editMenuEnabled = true;
  92.     static Boolean    communicateMenuEnabled = true;
  93. #if MACSHELL_VERSION
  94.     FileRecHndl        frHndl;
  95. #endif
  96.  
  97.     redrawMenuBar = false;
  98.     window = FrontWindow();
  99.     menu = GetMHandle(mFile);
  100.  
  101.     EnableItem(menu, iNew);                /* Set these for the no-windows state. */
  102.     EnableItem(menu, iOpen);
  103.     if (MaxBlock() < 0x10000L) {
  104.         if (CompactMem(0x10000L) < 0x10000L) {
  105.             DisableItem(menu, iNew);    /* Running low on RAM. */
  106.             DisableItem(menu, iOpen);
  107.         }
  108.     }
  109.  
  110.     DisableItem(menu, iClose);
  111.     DisableItem(menu, iSave);
  112.     DisableItem(menu, iSaveAs);
  113.     DisableItem(menu, iDuplicate);
  114.     DisableItem(menu, iPageSetup);
  115.     DisableItem(menu, iPrint);
  116.         /* Quit is always hilited. */
  117.  
  118.  
  119.  
  120.     if (IsDAWindow(window)) {
  121.         DisableItem(menu, iNew);    /* DAs don't get to do a new. */
  122.         DisableItem(menu, iOpen);    /* DAs don't get to do an open. */
  123.         EnableItem(menu, iClose);    /* Let DAs do a close from the menu. */
  124.     }
  125.  
  126.     if (IsAppWindow(window)) {
  127.         numWindows = GetWindowCount(false, false);
  128.         maxWindows = (numWindows < kMaxNumWindows);
  129.         EnableOrDisableItem(menu, iNew, maxWindows);
  130.         EnableOrDisableItem(menu, iOpen, maxWindows);
  131.             /* Allow a new and open only if max number of files not reached. */
  132.  
  133.         EnableItem(menu, iClose);
  134.         EnableOrDisableItem(menu, iSave, GetWindowDirty(window));
  135.         EnableItem(menu, iSaveAs);
  136.         EnableItem(menu, iDuplicate);
  137.  
  138.         EnableItem(menu, iPageSetup);
  139.         EnableItem(menu, iPrint);
  140.     }
  141.  
  142.  
  143.  
  144.     menu = GetMHandle(mEdit);
  145.     menuEnabled = false;
  146.     if (IsDAWindow(window)) {        /* A desk accessory may need edit menu. */
  147.         menuEnabled = true;
  148.         EnableItem(menu, iUndo);
  149.         EnableItem(menu, iCut);
  150.         EnableItem(menu, iCopy);
  151.         EnableItem(menu, iPaste);
  152.         EnableItem(menu, iClear);
  153.     }
  154. #if MACSHELL_VERSION
  155.     else CTEEditMenu(&menuEnabled, mEdit, iUndo, iCut);
  156. #endif
  157.     if (editMenuEnabled != menuEnabled) {
  158.         redrawMenuBar = true;
  159.         if (editMenuEnabled = menuEnabled) EnableItem(menu, 0);
  160.         else                               DisableItem(menu, 0);
  161.     }
  162.  
  163.  
  164. #if MACSHELL_VERSION
  165.  
  166.     menu = GetMHandle(mCommunicate);        /* Start by disabling everything. */
  167.     menuEnabled = false;
  168.     CheckItem(menu, iConnectToUser, false);
  169.     DisableItem(menu, iConnectToUser);
  170.     DisableItem(menu, iSendToUser);
  171.  
  172.     if (IsAppWindow(window)) {
  173.  
  174.         frHndl = (FileRecHndl)GetWRefCon(window);
  175.  
  176.         if (!(*frHndl)->fileState.readOnly) {
  177.  
  178.             if (gHasAppleEvents) {
  179.                 EnableItem(menu, iConnectToUser);
  180.                 menuEnabled = true;
  181.                 if ((*frHndl)->connect.connected) {
  182.                     DisableItem(menu, iConnectToUser);
  183.                     EnableItem(menu, iSendToUser);
  184.                 }
  185.                 CheckItem(menu, iConnectToUser, (*frHndl)->connect.connected);
  186.             }
  187.         }
  188.     }
  189.     if (communicateMenuEnabled != menuEnabled) {
  190.         redrawMenuBar = true;
  191.         if (communicateMenuEnabled = menuEnabled)
  192.              EnableItem(menu, 0);
  193.         else DisableItem(menu, 0);
  194.     }
  195.  
  196. #endif
  197.  
  198.     if (redrawMenuBar) DrawMenuBar();
  199. }
  200.  
  201.  
  202.  
  203. /*****************************************************************************/
  204.  
  205.  
  206.  
  207. /* This function either enables or disables a menu item. */
  208.  
  209. #pragma segment Menu
  210. void    EnableOrDisableItem(MenuHandle menu, short item, Boolean enable)
  211. {
  212.     if (enable) EnableItem(menu, item);
  213.     else        DisableItem(menu, item);
  214. }
  215.  
  216.  
  217.  
  218. /*****************************************************************************/
  219.  
  220.  
  221.  
  222. /* This is called when an item is chosen from the menu bar (after calling
  223. ** MenuSelect or MenuKey).  It performs the right operation for each command.
  224. ** It is good to have both the result of MenuSelect and MenuKey go to one
  225. ** routine like this to keep everything organized.
  226. */
  227.  
  228. #pragma segment Menu
  229. void    DoMenuCommand(long menuResult)
  230. {
  231.     short        menuID;            /* The resource ID of the selected menu. */
  232.     short        menuItem;        /* The item number of the selected menu. */
  233.     Str255        daName;
  234.     short        daRefNum;
  235.     FileRecHndl    frHndl, newFrHndl;
  236.     OSErr        err;
  237.     WindowPtr    window;
  238.  
  239.     if (window = FrontWindow())
  240.         frHndl = (FileRecHndl)GetWRefCon(window);
  241.             /* frHndl is valid only if it is one of our windows. */
  242.  
  243.     menuID = HiWord(menuResult);    /* Use macros for efficiency to get */
  244.     menuItem = LoWord(menuResult);    /* menu item number and menu number. */
  245.  
  246.     switch (menuID) {
  247.  
  248.         case mApple:
  249.             switch (menuItem) {
  250.                 case iAbout:    /* Bring up alert for About. */
  251.                     Alert(rAboutAlert, (ModalFilterProcPtr)keyEquivFilter);
  252.                     break;
  253.                 default:        /* All non-About items in this menu are DAs. */
  254.                     GetItem(GetMHandle(mApple), menuItem, daName);
  255.                     daRefNum = OpenDeskAcc(daName);
  256.                     break;
  257.             }
  258.             break;
  259.  
  260.         case mFile:
  261.             switch (menuItem) {
  262.                 case iNew:
  263.                     err = AppNewDocument(&frHndl, docFileType);
  264.                     if (!err) {
  265.                         if (err = DoNewWindow(frHndl, nil, (WindowPtr)-1, kwAppWindow))
  266.                             AppDisposeDocument(frHndl);
  267.                     }
  268.                     if (err) Alert(rErrorAlert, (ModalFilterProcPtr)alertFilter);
  269.                     break;
  270.                 case iOpen:
  271.                     err = AppOpenDocument(&frHndl, nil, fsRdWrPerm);
  272.                     if (!err) {
  273.                         if (err = DoNewWindow(frHndl, nil, (WindowPtr)-1, kwAppWindow))
  274.                             AppDisposeDocument(frHndl);
  275.                     }
  276.                     if ((err) && (err != userCanceledErr))
  277.                         Alert(rErrorAlert, (ModalFilterProcPtr)alertFilter);
  278.                     break;
  279.                 case iClose:
  280.                     DisposeOneWindow(window, iClose);
  281.                     break;
  282.                 case iSave:
  283.                     err = AppSaveDocument(frHndl, window, iSave);
  284.                     if ((err) && (err != userCanceledErr))
  285.                         Alert(rErrorAlert, (ModalFilterProcPtr)alertFilter);
  286.                     break;
  287.                 case iSaveAs:
  288.                     err = AppSaveDocument(frHndl, window, iSaveAs);
  289.                     if ((err) && (err != userCanceledErr))
  290.                         Alert(rErrorAlert, (ModalFilterProcPtr)alertFilter);
  291.                     break;
  292.                 case iDuplicate:
  293.                     err = AppDuplicateDocument(frHndl, &newFrHndl);
  294.                     if (!err) {
  295.                         if (err = DoNewWindow(newFrHndl, nil, (WindowPtr)-1, kwAppWindow))
  296.                             AppDisposeDocument(newFrHndl);
  297.                     }
  298.                     if (err) Alert(rErrorAlert, (ModalFilterProcPtr)alertFilter);
  299.                     break;
  300.                 case iPageSetup:
  301.                     DoSetCursor(&qd.arrow);
  302.                     PresentStyleDialog(frHndl);
  303.                     break;
  304.                 case iPrint:
  305.                     DoSetCursor(&qd.arrow);
  306.                     err = noErr;
  307.                     if (!(*frHndl)->doc.printRecValid)
  308.                         err = PresentStyleDialog(frHndl);
  309.                     if (!err) {
  310.                         err = AppPrintDocument(frHndl, true, true);
  311.                         AppPrintDocument(nil, false, false);
  312.                     }
  313.                     if ((err) && (err != userCanceledErr))
  314.                         Alert(rErrorAlert, (ModalFilterProcPtr)alertFilter);
  315.                     break;
  316.                 case iQuit:
  317.                     gQuitApplication = DisposeAllWindows();
  318.                     break;
  319.             }
  320.             break;
  321.  
  322.         case mEdit:            /* Call SystemEdit for DA editing & MultiFinder. */
  323.             if (IsAppWindow(window)) {
  324. #if MACSHELL_VERSION
  325.                 switch (menuItem) {
  326.                     case iUndo:
  327.                     case iCut:
  328.                     case iCopy:
  329.                     case iPaste:
  330.                     case iClear:
  331.                         BeginContent(window);
  332.                         if (menuItem == iUndo)
  333.                             CTEUndo();
  334.                         else
  335.                             CTEClipboard(menuItem - iCut + 2);
  336.                         EndContent(window);
  337.                         break;
  338.                 }
  339.                 if (menuItem != iCopy) (*frHndl)->fileState.docDirty = true;
  340. #endif
  341.             }
  342.             else SystemEdit(menuItem - 1);
  343.             break;
  344.  
  345. #if MACSHELL_VERSION
  346.         case mCommunicate:
  347.             switch (menuItem) {
  348.                 case iConnectToUser:
  349.                     DoSetCursor(&qd.arrow);
  350.                     if ((*frHndl)->connect.connected) SendMessage(frHndl, kDisconnectMssg);
  351.                     else                              SendConnect(frHndl);
  352.                     break;
  353.                 case iSendToUser:
  354.                     SendMessage(frHndl, kTextMssg);
  355.                     break;
  356.  
  357.             }
  358.             break;
  359. #endif
  360.  
  361.     }
  362.  
  363.     HiliteMenu(0);        /* Unhighlight what MenuSelect (or MenuKey) hilited. */
  364. }
  365.  
  366.  
  367.  
  368.